/**
 * 
 */
package no.hio.wsclient.dao;


import junit.framework.TestCase;
import no.hio.wsproviderproduct.Product;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Tests various operations on the web-service.
 * @author andreas
 *
 */
public class ProductDAOWebServiceImplTestCase extends TestCase{
	
	ProductDAOWebServiceImpl dao;
	String p1Title = "Product1";
	String p2Title = "Product2";
	int p1Price = 10;
	int p2Price = 20;
	//int p1ID, p2ID;
	Product p1, p2;
	
	@Before
	public void setUp() throws Exception {
		p1 = new Product();
		p2 = new Product();
		dao = new ProductDAOWebServiceImpl();
		p1.setTitle(p1Title);
		p2.setTitle(p2Title);
		/**p1.setDescription(p1Title);
		p2.setDescription(p2Title);
		p1.setPrice(p1Price);
		p2.setPrice(p2Price);**/
	}
	
	@Test 
	public void testInsert()
	{
		int p1ID = dao.insertProduct(p1);
		p1.setProductID(p1ID);
		
		assertTrue("Skulle vært vellykket", p1ID > 0);
		
		//prøver å sette inn produkt med eksisterende ID
		p2.setProductID(p1ID);
		int p2ID = dao.insertProduct(p2);
		
		assertFalse("Skulle være mislykket", p2ID > 0);
		
	}
	
	@Test
	public void testUpdate(){
		
		int p1id = dao.insertProduct(p1);
		p1.setProductID(p1id);
		String newTitle = "New Product Title";
		p1.setTitle(newTitle);
		
		dao.updateProduct(p1);
		Product updatedProduct = dao.findByProductID(p1.getProductID());
		
		assertEquals("Produktet er ikke oppdatert", p1.getTitle(), updatedProduct.getTitle());
		
		
		
	}
	
	@Test
	public void testFindAllProducts()
	{
		assertTrue(dao.findAllProducts().size() >= 0);
	}
	
	@Test
	public void testFindByProductID()
	{
		int p1id = dao.insertProduct(p1);
		p1.setProductID(p1id);
		
		Product found = dao.findByProductID(p1.getProductID());
		assertEquals("Produktet ble funnet", p1.getProductID(), found.getProductID());
		
		
	}
	
	@Test
	public void testRemoveProduct()
	{
		int p1id = dao.insertProduct(p1);
		p1.setProductID(p1id);
		
		dao.removeProduct(p1);
		
		assertNull("skulle vært null", dao.findByProductID(p1id));
	}

	@After
	public void tearDown() throws Exception {
		dao.removeProduct(p1);
		dao.removeProduct(p2);
	}

}
